// TODO: replace with wxPreferencesEditor

#include "settings_dialog.h"
#include "settings.h"
#include "render.h"
#include "util.h"

//(*InternalHeaders(SettingsDialog)
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/checkbox.h>
#include <wx/aui/aui.h>
#include <wx/statline.h>
#include <wx/slider.h>
#include <wx/panel.h>
#include <wx/choice.h>
#include <wx/intl.h>
#include <wx/button.h>
#include <wx/fontpicker.h>
#include <wx/string.h>
#include <wx/clrpicker.h>
//*)

//(*IdInit(SettingsDialog)
const long SettingsDialog::ID_STATICTEXT3 = wxNewId();
const long SettingsDialog::ID_TEXTCTRL1 = wxNewId();
const long SettingsDialog::ID_STATICTEXT4 = wxNewId();
const long SettingsDialog::ID_STATICTEXT5 = wxNewId();
const long SettingsDialog::ID_SCROLL_SPEED = wxNewId();
const long SettingsDialog::ID_PANEL1 = wxNewId();
const long SettingsDialog::ID_STATICTEXT1 = wxNewId();
const long SettingsDialog::ID_STATICLINE1 = wxNewId();
const long SettingsDialog::ID_STATICTEXT2 = wxNewId();
const long SettingsDialog::ID_FONTPICKERCTRL1 = wxNewId();
const long SettingsDialog::ID_UNDERLINED = wxNewId();
const long SettingsDialog::ID_STATICLINE2 = wxNewId();
const long SettingsDialog::ID_CHECKBOX2 = wxNewId();
const long SettingsDialog::ID_CHECKBOX3 = wxNewId();
const long SettingsDialog::ID_COLOURPICKERCTRL1 = wxNewId();
const long SettingsDialog::ID_PANEL2 = wxNewId();
const long SettingsDialog::ID_PANEL3 = wxNewId();
const long SettingsDialog::ID_AUINOTEBOOK1 = wxNewId();
const long SettingsDialog::ID_RESET_DEFAULTS = wxNewId();
//*)

BEGIN_EVENT_TABLE(SettingsDialog,wxDialog)
	//(*EventTable(SettingsDialog)
	//*)
END_EVENT_TABLE()

SettingsDialog::SettingsDialog(wxWindow* parent)
{
	//(*Initialize(SettingsDialog)
	wxBoxSizer* BoxSizer4;
	wxBoxSizer* BoxSizer5;
	wxButton* button_ok;
	wxBoxSizer* sizer_buttons;
	wxFlexGridSizer* FlexGridSizer2;
	wxBoxSizer* BoxSizer2;
	wxButton* button_cancel;
	wxBoxSizer* BoxSizer1;
	wxBoxSizer* top_sizer;
	wxStaticBoxSizer* StaticBoxSizer1;
	wxFlexGridSizer* FlexGridSizer1;
	wxBoxSizer* BoxSizer3;

	Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxCAPTION|wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER, _T("wxID_ANY"));
	SetClientSize(wxSize(754,417));
	top_sizer = new wxBoxSizer(wxVERTICAL);
	AuiNotebook1 = new wxAuiNotebook(this, ID_AUINOTEBOOK1, wxDefaultPosition, wxSize(0,0), wxAUI_NB_DEFAULT_STYLE);
	AuiNotebook1->SetMinSize(wxSize(1000,500));
	Panel1 = new wxPanel(AuiNotebook1, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
	BoxSizer5 = new wxBoxSizer(wxVERTICAL);
	FlexGridSizer2 = new wxFlexGridSizer(0, 3, 0, 0);
	FlexGridSizer2->AddGrowableCol(1);
	StaticText3 = new wxStaticText(Panel1, ID_STATICTEXT3, _("&Language"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT3"));
	FlexGridSizer2->Add(StaticText3, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	lang_box = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	lang_box->SetMinSize(wxSize(200,-1));
	FlexGridSizer2->Add(lang_box, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	lang_desc_box = new wxStaticText(Panel1, ID_STATICTEXT4, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT4"));
	lang_desc_box->SetMinSize(wxSize(200,-1));
	FlexGridSizer2->Add(lang_desc_box, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText4 = new wxStaticText(Panel1, ID_STATICTEXT5, _("&Scroll speed"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT5"));
	FlexGridSizer2->Add(StaticText4, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	scroll_speed = new wxSlider(Panel1, ID_SCROLL_SPEED, 0, 1, 30, wxDefaultPosition, wxDefaultSize, wxSL_LABELS, wxDefaultValidator, _T("ID_SCROLL_SPEED"));
	FlexGridSizer2->Add(scroll_speed, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer5->Add(FlexGridSizer2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Panel1->SetSizer(BoxSizer5);
	BoxSizer5->Fit(Panel1);
	BoxSizer5->SetSizeHints(Panel1);
	Panel2 = new wxPanel(AuiNotebook1, ID_PANEL2, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL2"));
	StaticBoxSizer1 = new wxStaticBoxSizer(wxVERTICAL, Panel2, _("Appearance"));
	BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
	StaticText1 = new wxStaticText(Panel2, ID_STATICTEXT1, _("&Element"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
	BoxSizer3->Add(StaticText1, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	font_choice = new wxChoice(Panel2, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("wxID_ANY"));
	font_choice->SetMinSize(wxSize(250,-1));
	BoxSizer3->Add(font_choice, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticLine1 = new wxStaticLine(Panel2, ID_STATICLINE1, wxDefaultPosition, wxDefaultSize, wxLI_VERTICAL, _T("ID_STATICLINE1"));
	BoxSizer3->Add(StaticLine1, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer4 = new wxBoxSizer(wxVERTICAL);
	BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
	StaticText2 = new wxStaticText(Panel2, ID_STATICTEXT2, _("&Font"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
	BoxSizer1->Add(StaticText2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	font_picker = new wxFontPickerCtrl(Panel2, ID_FONTPICKERCTRL1, wxNullFont, wxDefaultPosition, wxDefaultSize, wxFNTP_FONTDESC_AS_LABEL|wxFNTP_USEFONT_FOR_LABEL, wxDefaultValidator, _T("ID_FONTPICKERCTRL1"));
	BoxSizer1->Add(font_picker, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer4->Add(BoxSizer1, 0, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
	style_bold = new wxCheckBox(Panel2, wxID_BOLD, _("&Bold"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("wxID_BOLD"));
	style_bold->SetValue(false);
	BoxSizer2->Add(style_bold, 0, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	style_italic = new wxCheckBox(Panel2, wxID_ITALIC, _("&Italic"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("wxID_ITALIC"));
	style_italic->SetValue(false);
	BoxSizer2->Add(style_italic, 0, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	style_underlined = new wxCheckBox(Panel2, ID_UNDERLINED, _("&Underlined"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_UNDERLINED"));
	style_underlined->SetValue(false);
	BoxSizer2->Add(style_underlined, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer4->Add(BoxSizer2, 0, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer3->Add(BoxSizer4, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticLine2 = new wxStaticLine(Panel2, ID_STATICLINE2, wxDefaultPosition, wxDefaultSize, wxLI_VERTICAL, _T("ID_STATICLINE2"));
	BoxSizer3->Add(StaticLine2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer1 = new wxFlexGridSizer(0, 2, 0, 0);
	fg_color_enable = new wxCheckBox(Panel2, ID_CHECKBOX2, _("Fo&reground color"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX2"));
	fg_color_enable->SetValue(false);
	FlexGridSizer1->Add(fg_color_enable, 0, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	fg_color_picker = new wxColourPickerCtrl(Panel2, wxID_ANY, wxColour(0,0,0), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("wxID_ANY"));
	FlexGridSizer1->Add(fg_color_picker, 0, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	bg_color_enable = new wxCheckBox(Panel2, ID_CHECKBOX3, _("Backgroun&d color"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX3"));
	bg_color_enable->SetValue(false);
	FlexGridSizer1->Add(bg_color_enable, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	bg_color_picker = new wxColourPickerCtrl(Panel2, ID_COLOURPICKERCTRL1, wxColour(0,0,0), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_COLOURPICKERCTRL1"));
	FlexGridSizer1->Add(bg_color_picker, 0, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer3->Add(FlexGridSizer1, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer3->Add(-1,-1,0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticBoxSizer1->Add(BoxSizer3, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	text_example = new wxTextCtrl(Panel2, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(1000,363), wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH, wxDefaultValidator, _T("wxID_ANY"));
	text_example->SetMinSize(wxSize(550,300));
	StaticBoxSizer1->Add(text_example, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Panel2->SetSizer(StaticBoxSizer1);
	StaticBoxSizer1->Fit(Panel2);
	StaticBoxSizer1->SetSizeHints(Panel2);
	Panel3 = new wxPanel(AuiNotebook1, ID_PANEL3, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL3"));
	AuiNotebook1->AddPage(Panel1, _("General"));
	AuiNotebook1->AddPage(Panel2, _("Appearance"));
	AuiNotebook1->AddPage(Panel3, _("a"));
	top_sizer->Add(AuiNotebook1, 4, wxALL|wxEXPAND|wxFIXED_MINSIZE|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	sizer_buttons = new wxBoxSizer(wxHORIZONTAL);
	reset_defaults = new wxButton(this, ID_RESET_DEFAULTS, _("&Reset defaults"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_RESET_DEFAULTS"));
	sizer_buttons->Add(reset_defaults, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	sizer_buttons->Add(-1,-1,1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	button_ok = new wxButton(this, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("wxID_OK"));
	sizer_buttons->Add(button_ok, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	button_cancel = new wxButton(this, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("wxID_CANCEL"));
	sizer_buttons->Add(button_cancel, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	top_sizer->Add(sizer_buttons, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	SetSizer(top_sizer);
	SetSizer(top_sizer);
	Layout();

	Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&SettingsDialog::OnLanguage);
	Connect(ID_SCROLL_SPEED,wxEVT_SCROLL_TOP|wxEVT_SCROLL_BOTTOM|wxEVT_SCROLL_LINEUP|wxEVT_SCROLL_LINEDOWN|wxEVT_SCROLL_PAGEUP|wxEVT_SCROLL_PAGEDOWN|wxEVT_SCROLL_THUMBTRACK|wxEVT_SCROLL_THUMBRELEASE|wxEVT_SCROLL_CHANGED,(wxObjectEventFunction)&SettingsDialog::OnScrollSpeed);
	Connect(wxID_ANY,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&SettingsDialog::OnFontChoice);
	Connect(ID_FONTPICKERCTRL1,wxEVT_COMMAND_FONTPICKER_CHANGED,(wxObjectEventFunction)&SettingsDialog::OnFontPicker);
	Connect(wxID_BOLD,wxEVT_COMMAND_CHECKBOX_CLICKED,(wxObjectEventFunction)&SettingsDialog::OnBold);
	Connect(wxID_ITALIC,wxEVT_COMMAND_CHECKBOX_CLICKED,(wxObjectEventFunction)&SettingsDialog::OnItalic);
	Connect(ID_UNDERLINED,wxEVT_COMMAND_CHECKBOX_CLICKED,(wxObjectEventFunction)&SettingsDialog::OnUnderline);
	Connect(ID_CHECKBOX2,wxEVT_COMMAND_CHECKBOX_CLICKED,(wxObjectEventFunction)&SettingsDialog::OnFgColorEnable);
	Connect(wxID_ANY,wxEVT_COMMAND_COLOURPICKER_CHANGED,(wxObjectEventFunction)&SettingsDialog::OnFgColorPicker);
	Connect(ID_CHECKBOX3,wxEVT_COMMAND_CHECKBOX_CLICKED,(wxObjectEventFunction)&SettingsDialog::OnBgColorEnable);
	Connect(ID_COLOURPICKERCTRL1,wxEVT_COMMAND_COLOURPICKER_CHANGED,(wxObjectEventFunction)&SettingsDialog::OnBgColorPicker);
	Panel3->Connect(wxEVT_PAINT,(wxObjectEventFunction)&SettingsDialog::OnPanel3Paint,0,this);
	Connect(ID_RESET_DEFAULTS,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&SettingsDialog::OnResetDefaults);
	Connect(wxID_OK,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&SettingsDialog::OnOK);
	Connect(wxID_CANCEL,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&SettingsDialog::OnCancel);
	//*)

    example_note.date = "1913-01-10";
    example_note.caption = "Note caption";
    example_note.text = wxString(_(
         "Some Common text.\nSome Common text with source (Book, s. 123)\nText with known keyword {<<--known-keyword--}, unknown keyword {<<--unknown-keyword--}, incorrect keyword {incorrect @ keyword}\n"
        ));
    config->render_options.collapse = false;
    config->render_options.show_sources = true;
    config->render_options.show_keyword_marks = false;
    config->render_options.show_unknown_keywords = true;

    for (int x=0; x<FONT_MAX; x++)
        font_choice->Append(config->fonts[x].name);

    LoadFromConfig();
    SetFocus();     // without this, ESC doesn't work initially on wxGTK 2.8

}

void SettingsDialog::LoadFromConfig()
{
    // dummy event object, because programmatic setting of values usually doesn't generate event
    wxCommandEvent event;

    scroll_speed->SetValue(config->scroll_speed);
    lang_box->SetValue(config->lang);

    font_choice->SetSelection(0);
    OnFontChoice(event);
    UpdateExample();
}

SettingsDialog::~SettingsDialog()
{
	//(*Destroy(SettingsDialog)
	//*)
}


void SettingsDialog::OnOK(wxCommandEvent& event)
{
    // language changed?
    wxString new_lang = lang_box->GetValue();
    if (new_lang != config->lang) {
        if (!config->CheckLangString(new_lang)) {
            ErrMsg(_("Invalid language '%s'"), new_lang);
            lang_box->SetFocus();
            return;
        }
        WarnMsg(_("Changing language requires application restart"));
        config->lang = new_lang;
    }

    EndModal(1);
}

void SettingsDialog::OnCancel(wxCommandEvent& event)
{
    EndModal(0);
}

bool ShowSettingsDialog(wxWindow* parent)
{
    SettingsDialog dlg(parent);
    bool retval = dlg.ShowModal();
    return retval;
}

void SettingsDialog::UpdateFont(const wxFont& f)
{
    font_picker->SetSelectedFont(f);
    style_bold->SetValue(f.GetWeight() == wxFONTWEIGHT_BOLD);
    style_italic->SetValue(f.GetStyle() == wxFONTSTYLE_ITALIC);
    style_underlined->SetValue(f.GetUnderlined());
}

void SettingsDialog::OnFontChoice(wxCommandEvent& event)
{
    int font_id = font_choice->GetCurrentSelection();
    const wxTextAttr& attr = config->fonts[font_id].attr;
    UpdateFont(attr.GetFont());

    wxColour fg_color = attr.GetTextColour();
    wxColour default_fg_color = config->fonts[FONT_DEFAULT].attr.GetTextColour();
    if (!default_fg_color.IsOk()) default_fg_color = *wxBLACK;
    fg_color_enable->SetValue(fg_color.IsOk());
    fg_color_picker->Enable(fg_color.IsOk());
    fg_color_picker->SetColour(fg_color.IsOk() ? fg_color : default_fg_color);

    wxColour bg_color = attr.GetBackgroundColour();
    wxColour default_bg_color = config->fonts[FONT_DEFAULT].attr.GetBackgroundColour();
    if (!default_bg_color.IsOk()) default_bg_color = *wxWHITE;
    bg_color_enable->SetValue(bg_color.IsOk());
    bg_color_picker->Enable(bg_color.IsOk());
    bg_color_picker->SetColour(bg_color.IsOk() ? bg_color : default_bg_color);
}


void SettingsDialog::OnFontPicker(wxFontPickerEvent& event)
{
    wxFont new_font = font_picker->GetSelectedFont();

    // disallow light font weight
    if (new_font.GetWeight() == wxFONTWEIGHT_LIGHT)
        new_font.SetWeight(wxFONTWEIGHT_NORMAL);

    // if the default font was changed, change also every other element with same
    // font name and size
    int font_id = font_choice->GetCurrentSelection();
    wxASSERT(font_id < FONT_MAX);
    if (font_id == FONT_DEFAULT) {
        const wxFont font_orig = config->fonts[font_id].attr.GetFont();
        for (int x=0; x<FONT_MAX; x++) {
            wxFont f = config->fonts[x].attr.GetFont();
            if (0 == font_orig.GetFaceName().CmpNoCase(f.GetFaceName()) && font_orig.GetPointSize() == f.GetPointSize()) {
                f.SetFaceName(new_font.GetFaceName());
                f.SetPointSize(new_font.GetPointSize());
                config->fonts[x].attr.SetFont(f);
            }
        }

    // if it's not default font, just save selection
    } else {
        config->fonts[font_id].attr.SetFont(new_font);
    }

    // update controls
    UpdateFont(new_font);
    UpdateExample();
}

void SettingsDialog::UpdateExample()
{
    TextItem item(example_note);
    item.Render(text_example, config->render_options);
}

void SettingsDialog::OnFgColorEnable(wxCommandEvent& event)
{
    fg_color_picker->Enable(fg_color_enable->GetValue());

    int font_id = font_choice->GetCurrentSelection();
    wxASSERT(font_id < FONT_MAX);
    wxColour color = fg_color_enable->GetValue() ? fg_color_picker->GetColour() : wxColour();
    config->fonts[font_id].attr.SetTextColour(color);

    UpdateExample();
}

void SettingsDialog::OnFgColorPicker(wxColourPickerEvent& event)
{
    wxASSERT(fg_color_enable->GetValue() == true);

    int font_id = font_choice->GetCurrentSelection();
    wxASSERT(font_id < FONT_MAX);
    wxColour color = fg_color_picker->GetColour();
    config->fonts[font_id].attr.SetTextColour(color);

    UpdateExample();
}

void SettingsDialog::OnBgColorEnable(wxCommandEvent& event)
{
    bg_color_picker->Enable(bg_color_enable->GetValue());

    int font_id = font_choice->GetCurrentSelection();
    wxASSERT(font_id < FONT_MAX);
    wxColour color = bg_color_enable->GetValue() ? bg_color_picker->GetColour() : wxColour();
    config->fonts[font_id].attr.SetBackgroundColour(color);

    UpdateExample();
}

void SettingsDialog::OnBgColorPicker(wxColourPickerEvent& event)
{
    wxASSERT(bg_color_enable->GetValue() == true);

    int font_id = font_choice->GetCurrentSelection();
    wxASSERT(font_id < FONT_MAX);
    wxColour color = bg_color_picker->GetColour();
    config->fonts[font_id].attr.SetBackgroundColour(color);

    UpdateExample();
}

void SettingsDialog::OnBold(wxCommandEvent& event)
{
    int font_id = font_choice->GetCurrentSelection();
    wxASSERT(font_id < FONT_MAX);
    wxFont f = config->fonts[font_id].attr.GetFont();
    f.SetWeight(style_bold->GetValue() ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);

    font_picker->SetSelectedFont(f);
    config->fonts[font_id].attr.SetFont(f);
    UpdateExample();
}


void SettingsDialog::OnItalic(wxCommandEvent& event)
{
    int font_id = font_choice->GetCurrentSelection();
    wxASSERT(font_id < FONT_MAX);
    wxFont f = config->fonts[font_id].attr.GetFont();
    f.SetStyle(style_italic->GetValue() ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL);

    font_picker->SetSelectedFont(f);
    config->fonts[font_id].attr.SetFont(f);
    UpdateExample();
}

void SettingsDialog::OnUnderline(wxCommandEvent& event)
{
    int font_id = font_choice->GetCurrentSelection();
    wxASSERT(font_id < FONT_MAX);
    wxFont f = config->fonts[font_id].attr.GetFont();
    f.SetUnderlined(style_underlined->GetValue());

    font_picker->SetSelectedFont(f);
    config->fonts[font_id].attr.SetFont(f);
    UpdateExample();
}

void SettingsDialog::OnLanguage(wxCommandEvent& event)
{
    wxString lang = lang_box->GetValue();
    wxLanguageInfo langinfo;
    if (config->GetLangDesc(lang, &langinfo)) {
        lang_box->SetBackgroundColour(wxNullColour);
        lang_desc_box->SetLabel(langinfo.Description);
    } else {
        lang_box->SetBackgroundColour(*wxRED);
        lang_desc_box->SetLabel(_("Unknown language!"));
    }
}

void SettingsDialog::OnResetDefaults(wxCommandEvent& event)
{
    if (!Confirm(_("Are you sure you want to reset all settings to defaults?")))
        return;
    config->ResetDefaults();
    LoadFromConfig();
}

void SettingsDialog::OnScrollSpeed(wxScrollEvent& event)
{
    config->scroll_speed = scroll_speed->GetValue();
}

void SettingsDialog::OnPanel3Paint(wxPaintEvent& event)
{
}
